package com.stylefeng.guns.modular.system.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.InitCompanyID;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.CompanyMapper;
import com.stylefeng.guns.modular.system.model.Company;
import com.stylefeng.guns.modular.system.model.InitId;
import com.stylefeng.guns.modular.system.service.ICompanyService;
import com.stylefeng.guns.modular.system.service.IInitIdService;

/**
 * 公司资料控制器
 *
 * @author fengshuonan
 * @Date 2018-04-03 15:39:38
 */
@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

    private String PREFIX = "/system/company/";

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private CompanyMapper companyMapper;
    
  

    /**
     * 跳转到公司资料首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "company.html";
    }

    /**
     * 跳转到添加公司资料
     */
    @RequestMapping("/company_add")
    public String companyAdd() {
        return PREFIX + "company_add.html";
    }

    /**
     * 跳转到修改公司资料
     */
    @RequestMapping("/company_update/{companyId}")
    public String companyUpdate(@PathVariable Integer companyId, Model model) {
        Company company = companyService.selectById(companyId);
        model.addAttribute("item",company);
        LogObjectHolder.me().set(company);
        return PREFIX + "company_edit.html";
    }

    /**
     * 获取公司资料列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	Page<Company> page = new PageFactory<Company>().defaultPage();
//    	Page<Consignee> page = new PageFactory<Consignee>().defaultPage();
    	if(ToolUtil.isEmpty(condition)){
    		page.setRecords(companyMapper.selectPage(page, null));
    		return super.packForBT(page);
    	}else{
    		EntityWrapper<Company> entityWrapper = new EntityWrapper<>();
    		Wrapper<Company> wrapper = entityWrapper.like("consigner_name", condition);
    		page.setRecords(companyMapper.selectPage(page,wrapper));
    		return super.packForBT(page); 
    	}
    }

    /**
     * 新增公司资料
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Company company) {
    	company.setCompanyId(InitCompanyID.getInstance().initDisorderID());
        companyService.insert(company);
        return SUCCESS_TIP;
    }

    /**
     * 删除公司资料
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer companyId) {
        companyService.deleteById(companyId);
        return SUCCESS_TIP;
    }

    /**
     * 修改公司资料
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Company company) {
        companyService.updateById(company);
        return SUCCESS_TIP;
    }

    /**
     * 公司资料详情
     */
    @RequestMapping(value = "/detail/{companyId}")
    @ResponseBody
    public Object detail(@PathVariable("companyId") Integer companyId) {
        return companyService.selectById(companyId);
    }
}
